package server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 * Created by 10188 on 2018/3/6.
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {

        // 收到消息直接打印输出
        System.out.println(ctx.channel().remoteAddress()+ " Say : "+ s);

        // 返回客户端信息 - 我已经接收到了你的消息
        // writeAndFlush写消息并发送
        //ctx.writeAndFlush("received your message !\n");
        //ctx.writeAndFlush();
    }

    // 当Channel变成活跃状态时被调用
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("RamoteAddress : "+ ctx.channel().remoteAddress()+ " active !");

        ctx.writeAndFlush("welcome to "+ InetAddress.getLocalHost().getHostName() + " server.\n");

        super.channelActive(ctx);
    }
}
